package com.isunland.managesystem.ui;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.NavUtils;
import android.support.v7.app.ActionBar;
import android.view.MenuItem;
import android.view.View;
import android.widget.ListView;
import com.google.gson.Gson;
import com.isunland.managesystem.R;
import com.isunland.managesystem.adapter.ContractListAdapter;
import com.isunland.managesystem.base.BaseListFragment;
import com.isunland.managesystem.base.BaseVolleyActivity;
import com.isunland.managesystem.common.ApiConst;
import com.isunland.managesystem.entity.ContractList;
import com.isunland.managesystem.entity.ContractList.ContractDetail;
import com.isunland.managesystem.entity.CurrentUser;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class ContractListFragment extends BaseListFragment {

  protected static final String EXTRA_ORDER_ID = "com.isunland.managesystem.ui.EXTRA_ORDER_ID";
  protected static final String REQUEST_CONTRACT_ID =
      "com.isunland.managesystem.ui.REQUEST_CONTRACT_ID";
  protected static final String REQUEST_CONTRACT_CODE =
      "com.isunland.managesystem.ui.REQUEST_CONTRACT_CODE";
  protected static final String REQUEST_CONTRACT_NAME =
      "com.isunland.managesystem.ui.REQUEST_CONTRACT_NAME";
  protected String mOrderId;
  private ContractListAdapter mAdapter;

  @Override public void onCreate(Bundle savedInstanceState) {
    setHasOptionsMenu(true);
    BaseVolleyActivity activity = (BaseVolleyActivity) getActivity();
    if (activity != null) {
      ActionBar actionBar = activity.getSupportActionBar();
      if (actionBar != null) {
        if (NavUtils.getParentActivityName(getActivity()) != null) {
          actionBar.setDisplayHomeAsUpEnabled(true);
        }
        actionBar.setTitle(R.string.contractList);
      }
    }
    mOrderId = getArguments().getString(EXTRA_ORDER_ID);
    super.onCreate(savedInstanceState);
  }

  @Override public String getUrl() {
    return ApiConst.URL_CONTRACT_LIST;
  }

  @Override public HashMap<String, String> getparameters() {

    HashMap<String, String> params = new HashMap<String, String>();
    params.put("memberCode", CurrentUser.newInstance(getActivity()).getMemberCode());
    if (mOrderId != null) {
      params.put("moutbillIds", mOrderId);
    }
    params.put("ifAllpay", "F");
    return params;
  }

  @Override public void resolveResponse(String arg0) {
    ContractList contractList = new Gson().fromJson(arg0, ContractList.class);
    List<ContractDetail> contractDetail = contractList.getRows();
    setAdapter(contractDetail);
  }

  private void setAdapter(List<ContractDetail> contractDetail) {
    if (contractDetail == null) {
      contractDetail = new ArrayList<ContractList.ContractDetail>();
    }

    if (mAdapter == null) {
      mAdapter = new ContractListAdapter(getActivity(), contractDetail);
      setListAdapter(mAdapter);
      return;
    }
    mAdapter.clear();
    mAdapter.addAll(contractDetail);
    ((ContractListAdapter) getListAdapter()).notifyDataSetChanged();
  }

  @Override public void onListItemClick(ListView l, View v, int position, long id) {
    ContractDetail contractDetail = mAdapter.getItem(position - 1);
    String contractId = contractDetail.getId();
    String contractCode = contractDetail.getContractCode();
    String contractName = contractDetail.getContractName();
    Intent intent = new Intent();
    intent.putExtra(REQUEST_CONTRACT_ID, contractId);
    intent.putExtra(REQUEST_CONTRACT_CODE, contractCode);
    intent.putExtra(REQUEST_CONTRACT_NAME, contractName);
    getActivity().setResult(Activity.RESULT_OK, intent);
    getActivity().finish();
    super.onListItemClick(l, v, position, id);
  }

  public static Fragment newInstance(String orderId) {
    Bundle bundle = new Bundle();
    bundle.putString(EXTRA_ORDER_ID, orderId);
    ContractListFragment fragment = new ContractListFragment();
    fragment.setArguments(bundle);
    return fragment;
  }

  @Override public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
      /**
       * 响应返回按钮
       */
      case android.R.id.home:
        if (NavUtils.getParentActivityName(getActivity()) != null) {
          NavUtils.navigateUpFromSameTask(getActivity());
        }
        break;
      default:
        break;
    }
    return super.onOptionsItemSelected(item);
  }
}
